package cn.passlink.bletoothchat.mActivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import cn.passlink.bletoothchat.Entity.mUser;
import cn.passlink.bletoothchat.R;
import cn.passlink.bletoothchat.SqlTables.UserTableManager;
import cn.passlink.bletoothchat.SqlTables.MyDatabaseHelper;
import cn.passlink.bletoothchat.Tools.Location.GPSHelper;
import cn.passlink.bletoothchat.Tools.Location.WifiLocationHelper;
@SuppressLint("MissingPermission")
public class RegisterActivity extends AppCompatActivity {
    private mUser mUser;
    private SQLiteDatabase database;
    Context context;
    private MyDatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=this;
        setContentView(R.layout.activity_register);
        Button mButton=(Button) findViewById(R.id.register_button);
        TextView name=(TextView)  findViewById(R.id.reg_username);
        TextView acadmy=(TextView)  findViewById(R.id.reg_acadmy);
        TextView classes=(TextView)  findViewById(R.id.reg_classes);
        TextView grade=(TextView)  findViewById(R.id.reg_grade);
        TextView phone=(TextView)  findViewById(R.id.reg_phone);
        LinearLayout regBox=(LinearLayout)findViewById(R.id.reg_box);
        mUser=new mUser();
        databaseHelper = new MyDatabaseHelper(context,2);
        database=databaseHelper.getWritableDatabase();


        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUser.setUsername(name.getText().toString());
                mUser.setAcademy(acadmy.getText().toString());
                mUser.setGrade(grade.getText().toString()) ;
                mUser.setPhone(phone.getText().toString());
                mUser.setClasses(classes.getText().toString());
//                检查数据
                 if( checkInputs(mUser))
                {
                    UserTableManager mUserTableManager=new UserTableManager(database);
                    //写入数据库
                    mUserTableManager.addUser(mUser);
                    mUserTableManager.updateUser(mUser);
                    goToTargetActivity(RelationshipActivity.class);

                }else {

                    //测试最后要删除
                    Toast.makeText(context, "未注册！！！", Toast.LENGTH_SHORT).show();

                }
                }

        });


    }

    public void goToTargetActivity(Class targe) {
        Intent intent = new Intent(this, targe);
        startActivity(intent);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(database!=null){
            database.close();
        }
    }

    private boolean checkInputs(mUser mUser) {
        String username = mUser.getUsername();
        if (TextUtils.isEmpty(username) || username.length() > 8) {
            // 处理空输入或超过长度限制的情况，例如给出错误提示
            Toast.makeText(context, "名字最长为8", Toast.LENGTH_SHORT).show();
            return false;
        }

        String academy = mUser.getAcademy();
        if (TextUtils.isEmpty(academy) || academy.length() > 10) {
            // 处理空输入或超过长度限制的情况，例如给出错误提示
            Toast.makeText(context, "学院名称最长为10", Toast.LENGTH_SHORT).show();
            return false;
        }

        String gradeText = String.valueOf(mUser.getGrade());
        if (TextUtils.isEmpty(gradeText)) {
            // 处理空输入的情况，例如给出错误提示
            Toast.makeText(context, "年纪为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        int gradeValue = Integer.parseInt(gradeText);
        if (gradeValue >= 9999) {
            // 处理超过数值限制的情况，例如给出错误提示
            Toast.makeText(context, "年级小于9999", Toast.LENGTH_SHORT).show();
            return false;
        }

        String phone = mUser.getPhone();
        if (TextUtils.isEmpty(phone) || phone.length() != 11 || !TextUtils.isDigitsOnly(phone)) {
            // 处理不满足格式要求的情况，例如给出错误提示
            Toast.makeText(context, "电话号码为11位", Toast.LENGTH_SHORT).show();
            return false;
        }

        String classesText = String.valueOf(mUser.getClasses());
        if (TextUtils.isEmpty(classesText)) {
            // 处理空输入的情况，例如给出错误提示
            Toast.makeText(context, "班级为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        int classesValue = Integer.parseInt(classesText);
        if (classesValue >= 999) {
            // 处理超过数值限制的情况，例如给出错误提示
            Toast.makeText(context, "班级序号最大为999", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }
}